const { list } = require('../../api/index')

Page({
    data: {
        baseURL: '',
        token: '',
        resultList: [],
        searchValue: '',
        page: 1,
        limit: 10,
        hasMore: true,
        userInfo: null,
        tableName: ''
    },

    onLoad() {
        const baseURL = wx.getStorageSync('baseURL');
        const token = wx.getStorageSync('token');
        
        this.setData({
            baseURL: baseURL,
            token: token
        });

        // 获取用户信息
        const userInfo = wx.getStorageSync('userSession');
        const tableName = wx.getStorageSync('nowTable');
        console.log('当前用户信息:', userInfo);
        console.log('当前用户角色:', tableName);
        this.setData({
            userInfo: userInfo,
            tableName: tableName
        });

        this.getAchievementList();
    },

    onShow: function() {
        this.getAchievementList();
    },

    getAchievementList: function() {
        const that = this;
        
        // 只查当前创业者自己的成果，筛选字段用xuehao
        const requestData = {
            page: 1,
            limit: 10,
            xuehao: that.data.userInfo.xuehao
        };
        
        console.log('【调试】请求创新成果列表:', {
            url: that.data.baseURL + '/xiangmuchengguo/page',
            method: 'POST',
            header: { 'Token': that.data.token },
            data: requestData,
            tableName: that.data.tableName
        });
        wx.request({
            url: that.data.baseURL + '/xiangmuchengguo/page',
            method: 'POST',
            header: {
                'Token': that.data.token
            },
            data: requestData,
            success: function(res) {
                console.log('【调试】创新成果列表响应:', res);
                console.log('【调试】响应数据结构:', res.data);
                console.log('【调试】数据列表:', res.data.data?.list);
                console.log('【调试】列表长度:', res.data.data?.list?.length);
                
                if (res.data.code === 0 && res.data.data && Array.isArray(res.data.data.list)) {
                    console.log('【调试】开始获取详情，列表长度:', res.data.data.list.length);
                    // 获取每个成果的详细信息
                    const promises = res.data.data.list.map(item => {
                        return new Promise((resolve) => {
                            wx.request({
                                url: that.data.baseURL + '/xiangmuchengguo/detail/' + item.id,
                                method: 'GET',
                                header: {
                                    'Token': that.data.token
                                },
                                success: function(detailRes) {
                                    console.log('【调试】成果详情响应:', detailRes);
                                    if (detailRes.data.code === 0) {
                                        resolve(detailRes.data.data);
                                    } else {
                                        resolve(item);
                                    }
                                },
                                fail: function(err) {
                                    console.error('【调试】获取成果详情失败:', err);
                                    resolve(item);
                                }
                            });
                        });
                    });

                    Promise.all(promises).then(details => {
                        console.log('【调试】最终设置的数据:', details);
                        console.log('【调试】数据长度:', details.length);
                        // 添加详细的数据调试信息
                        details.forEach((item, index) => {
                            console.log(`【调试】第${index + 1}条数据:`, {
                                id: item.id,
                                chengguomingcheng: item.chengguomingcheng,
                                shenhezhuangtai: item.shenhezhuangtai,
                                huodeshijian: item.huodeshijian,
                                chengguofenlei: item.chengguofenlei,
                                fuzerenzhanghao: item.fuzerenzhanghao
                            });
                        });
                        that.setData({
                            resultList: details
                        });
                    });
                } else {
                    console.warn('【调试】获取数据失败，响应内容:', res.data);
                    console.warn('【调试】code:', res.data.code);
                    console.warn('【调试】data:', res.data.data);
                    console.warn('【调试】list:', res.data.data?.list);
                    wx.showToast({
                        title: '获取数据失败',
                        icon: 'none'
                    });
                }
            },
            fail: function(error) {
                console.error('【调试】获取创新成果列表失败:', error);
                wx.showToast({
                    title: '网络错误',
                    icon: 'none'
                });
            }
        });
    },

    onSearchInput: function(e) {
        this.setData({
            searchValue: e.detail.value
        });
    },

    onSearch: function() {
        const that = this;
        const searchValue = this.data.searchValue;
        
        wx.request({
            url: that.data.baseURL + '/xiangmuchengguo/page',
            method: 'GET',
            header: {
                'Token': that.data.token
            },
            data: {
                page: 1,
                limit: 10,
                chengguomingcheng: searchValue
            },
            success: function(res) {
                if (res.data.code === 0) {
                    that.setData({
                        resultList: res.data.data.list
                    });
                }
            }
        });
    },

    toDetail: function(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/xiangmuchengguo/xiangmuchengguo?id=' + id
        });
    },

    toEdit: function(e) {
        const id = e.currentTarget.dataset.id;
        console.log('点击修改按钮，ID:', id);
        wx.navigateTo({
            url: '/pages/xiangmuchengguo/update-and-add?id=' + id + '&type=edit',
            success: function() {
                console.log('跳转到修改页面成功');
            },
            fail: function(error) {
                console.error('跳转到修改页面失败:', error);
                wx.showToast({
                    title: '页面跳转失败',
                    icon: 'none'
                });
            }
        });
    },

    toAdd: function() {
        wx.navigateTo({
            url: '/pages/xiangmuchengguo/xiangmuchengguo'
        });
    },

    onAddClick: function() {
        wx.navigateTo({
            url: '/pages/xiangmuchengguo/update-and-add?type=add',
            success: function() {
                console.log('跳转到添加成果页面成功');
            },
            fail: function(error) {
                console.error('跳转到添加成果页面失败:', error);
                wx.showToast({
                    title: '页面跳转失败',
                    icon: 'none'
                });
            }
        });
    },

    // 下拉刷新
    onPullDownRefresh() {
        this.getAchievementList()
        wx.stopPullDownRefresh()
    },

    // 上拉加载更多
    onReachBottom() {
        if (this.data.hasMore) {
            this.setData({
                page: this.data.page + 1
            })
            this.getAchievementList()
        }
    }
}) 